package com.lzj.myvideo.controller;

import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.lzj.myvideo.common.ServerResponse;
import com.lzj.myvideo.service.BgmService;
import com.lzj.myvideo.service.VideoServer;
import com.shortvideo.pojo.videos;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
public class VideoController {

    @Autowired
    private BgmService bgmService;

    @Autowired
    private VideoServer videoServer;

    /**
     * 主页显示视频
     * @param pageNum
     * @return
     */
    @RequestMapping("showVideo")
    public ServerResponse showVideo(Integer pageNum){
        if (pageNum == null){
            pageNum = 1;
        }
        ServerResponse serverResponse = videoServer.queryVideo(pageNum);
        PageInfo pageInfo = (PageInfo)serverResponse.getData();
        System.out.println(pageInfo);
        return ServerResponse.createBySuccess(pageInfo);
    }

    /**
     * 根据视频id查找视频并返回  返回单个
     * @param videoId
     * @return
     */
    @RequestMapping("queryVideoByVideoId")
    public ServerResponse queryVideoByVideoId(@RequestParam(value = "id") String videoId){
        if (StringUtils.isBlank(videoId) || videoId ==null){
            return ServerResponse.createByError("视频不存在");
        }
        ServerResponse serverResponse = videoServer.queryVideoByVideoId(videoId);
        if (!serverResponse.isSuccess()){
            return ServerResponse.createByError("查询失败");
        }
        return ServerResponse.createBySuccess(serverResponse.getData());
    }

    /**
     * 根据视频id查找视频并返回 返回多个
     * @param videos
     * @return
     */
    @RequestMapping("queryVideosByVideoId")
    public ServerResponse queryVideosByVideoId(String videos){
        System.out.println(videos);
        String s = videos.replaceAll("\\[", "").replaceAll("\\]", "").replaceAll("\"","");
        String[] newStr = s.split(",");
        ArrayList<String> list = Lists.newArrayList();
        for (String string : newStr) {
            list.add(string);
            System.out.println(string);
        }
        ServerResponse serverResponse = videoServer.queryVideosByVideoId(list);
        return ServerResponse.createBySuccess(serverResponse.getData());
    }

    /**
     * 查询用户的记录
     * @param userId
     * @return
     */
    @RequestMapping("queryVideoRecord")
    public ServerResponse queryVideoRecord(@RequestParam(value = "userId") String userId){
        if (StringUtils.isBlank(userId)) {
            return ServerResponse.createByError("userId为空");
        }
        List<String> videoIds = videoServer.queryVideoRecord(userId);

        return ServerResponse.createBySuccess(videoIds);

    }

    @RequestMapping("addVideoRecord")
    public ServerResponse addVideoRecord(@RequestParam(value = "userId") String userId,
                                           @RequestParam(value = "videoId") String videoId){
        if (StringUtils.isBlank(videoId) || videoId ==null){
            return ServerResponse.createByError("视频不存在");
        }
        if (StringUtils.isBlank(userId)) {
            return ServerResponse.createByError("userId为空");
        }
        videoServer.addVideoRecord(userId,videoId);
        return ServerResponse.createBySuccess();

    }

    /**
     * 根据用户id查找视频并返回
     * @param userId
     * @return
     */
    @RequestMapping("queryVideoByUserId")
    public ServerResponse queryVideoByUserId(@RequestParam(value = "id") String userId){
        if (StringUtils.isBlank(userId)) {
            return ServerResponse.createByError("userId为空");
        }
        ServerResponse serverResponse = videoServer.queryVideoByUserId(userId);
        if (!serverResponse.isSuccess()){
            return ServerResponse.createByError("查询失败");
        }
        return ServerResponse.createBySuccess(serverResponse.getData());
    }

    /**
     * 1.上传视频
     * 2.判断是否选中bgm，选中则和视频合并
     * 3.获取视频封面,返回视频封面地址
     * @param userId
     * @param bgmId
     * @param seconds
     * @param desc
     * @param width
     * @param height
     * @param file
     * @return
     */
    @RequestMapping("videoUpload")
    public ServerResponse videoUpload(@RequestParam("userId") String userId,
                                      @RequestParam(value = "bgmId",required = false) String bgmId,
                                      @RequestParam("seconds") String seconds,
                                      @RequestParam("desc") String desc,
                                      @RequestParam("width") String width,
                                      @RequestParam("height") String height,
                                      @RequestParam("file") MultipartFile file) {

        if (StringUtils.isBlank(userId)) {
            return ServerResponse.createByError("userId为空");
        }
        if (file == null){
            return ServerResponse.createByError("文件为空");
        }
        videos video = new videos();
        //上传视频
        ServerResponse videoResponse = videoServer.videoUpload(userId, file);
        System.out.println(bgmId);
        if (StringUtils.isNotBlank(bgmId)){
            //上传并合并视频bgm
            ServerResponse bgmResponse = videoServer.BgmUpload(userId, file ,bgmId,seconds);

            video.setAudio_id(bgmId);
        }else {
            video.setAudio_id("原生");
        }
        //设置视频封面
        videoServer.coverUpload(userId, file);
        video.setUser_id(userId);
        video.setVideo_seconds((int)Double.parseDouble(seconds));
        video.setAudio_desc(desc);
        video.setVideo_height(Integer.parseInt(height));
        video.setVideo_width(Integer.parseInt(width));
        video.setCreate_time(new Date());
        //保存数据到数据库中
        ServerResponse saveResponse = videoServer.saveVideo(video);
        if (saveResponse.isSuccess() == false){
            return ServerResponse.createByError(saveResponse.getMsg());
        }
        return ServerResponse.createBySuccess("上传成功",saveResponse.getData());
    }


    /**
     * 查询用户是否点赞过该视频
     * @param userId
     * @param videoId
     * @return
     */
    @RequestMapping("isLikeVideo")
    public ServerResponse isLikeVideo(@RequestParam("userId") String userId,
                                      @RequestParam("videoId")String videoId){
        if (StringUtils.equals(userId,"undefined") || userId == null){
            return ServerResponse.createByError("未登录");
        }
        Boolean islike = videoServer.islike(userId,videoId);

        return ServerResponse.createBySuccess(islike);
    }
    /**
     * 用户点赞视频
     * @param userId
     * @param videoId
     * @return
     */
    @RequestMapping("likeVideo")
    public ServerResponse likeVideo(String userId,String videoId){
        if (StringUtils.equals(userId,"undefined") || userId == null){
            return ServerResponse.createByError("未登录");
        }
        ServerResponse serverResponse = videoServer.likeVideo(userId,videoId);

        if (!serverResponse.isSuccess()){
            return ServerResponse.createByError("点赞失败");
        }
        return ServerResponse.createBySuccess();
    }

    /**
     * 取消视频点赞
     * @param userId
     * @param videoId
     * @return
     */
    @RequestMapping("unlikeVideo")
    public ServerResponse unlikeVideo(String userId,String videoId){
        if (StringUtils.equals(userId,"undefined") || userId == null){
            return ServerResponse.createByError("未登录");
        }
        ServerResponse serverResponse = videoServer.unlikeVideo(userId,videoId);

        if (!serverResponse.isSuccess()){
            return ServerResponse.createByError("取消点赞失败");
        }
        return ServerResponse.createBySuccess();
    }

    @RequestMapping("queryUserLikeVideos")
    public ServerResponse queryUserLikeVideos(String userId){
        if (StringUtils.equals(userId,"undefined") || userId == null){
            return ServerResponse.createByError("未登录");
        }
        ServerResponse serverResponse = videoServer.queryUserLikeVideos(userId);
        return ServerResponse.createBySuccess(serverResponse.getData());
    }

    @RequestMapping("commentVideos")
    public ServerResponse commentVideos(String userId,String videoId,String content){
        ServerResponse serverResponse = videoServer.commentVideos(userId,videoId,content);
        return serverResponse;
    }

    @RequestMapping("queryVideoComments")
    public ServerResponse queryVideoComments(String videoId){
        if (StringUtils.isBlank(videoId)||videoId == null){
            return ServerResponse.createByError("videoId为空");
        }
        ServerResponse serverResponse = videoServer.queryVideoComments(videoId);
        return ServerResponse.createBySuccess(serverResponse.getData());
    }

}
